import axios from 'axios'

const api = axios.create({
  baseURL: 'http://121.54.189.93:5000', // 根据你的后端地址修改
  // baseURL: '/proxy', // 根据你的后端地址修改
  timeout: 5000
})

// 请求拦截器 - 添加token
api.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截器 - 处理错误
api.interceptors.response.use(response => {
  return response.data
}, error => {
  if (error.response.status === 401) {
    localStorage.removeItem('token')
    window.location.href = '/login'
  }
  return Promise.reject(error)
})

export default {
  // 认证相关
  register(data) {
    return api.post('/api/auth/register', data)
  },
  login(data) {
    return api.post('/api/auth/login', data)
  },
  
  // 评价相关
  submitEvaluation(data) {
    return api.post('/api/evaluations', data)
  },
  checkOrderExists(orderId) {
    return api.get(`/api/evaluations/check/${orderId}`)
  },
  getEvaluations() {
    return api.get('/api/evaluations')
  },
  
  // 订单相关
  sendEmail(data) {
    return api.post('/api/orders', data)
  }
}